/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package net.sf.log4jdbc;

import java.sql.Connection;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * The Class DataSourceSpyInterceptor.
 */
public class DataSourceSpyInterceptor implements MethodInterceptor {  
	  
    private RdbmsSpecifics rdbmsSpecifics = null;  
      
    private RdbmsSpecifics getRdbmsSpecifics(Connection conn) {  
        if(rdbmsSpecifics == null) {  
            rdbmsSpecifics = DriverSpy.getRdbmsSpecifics(conn);  
        }  
        return rdbmsSpecifics;  
    }  
      
    /* (non-Javadoc)
     * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
     */
    public Object invoke(MethodInvocation invocation) throws Throwable {  
        Object result = invocation.proceed();  
        if(SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()) {  
            if(result instanceof Connection) {  
                Connection conn = (Connection)result;  
                return new ConnectionSpy(conn,getRdbmsSpecifics(conn));  
            }  
        }  
        return result;  
    }  
  
}  
